<?php
/**
 * Created by PhpStorm.
 * User: lelouchms
 * Date: 2019/3/28
 * Time: 16:08
 */

namespace app\backend\controller\admin\v1;

use app\common\exception\CustomerException;
use app\common\exception\LoginException;
use app\common\exception\ValidateException;
use app\common\util\CommonUtil;
use app\common\util\RandomUtil;
use think\facade\Cache;
use think\facade\Env;

class Admin extends Base
{

    /**
     * 排除的查询字段
     * @var array
     */
    protected $expectFields = ["password", "slat"];

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new \app\common\model\Admin();
        $this->expectFields = ["token", "updatetime", "loginfailure", "password", "slat"];
    }

    public function add()
    {
        $requestData = $this->request->post();
        $validate = new \app\common\validate\Admin();
        $flag = $validate->scene("add")->check($requestData);
        if ($flag === false) {
            throw new ValidateException($validate->getError());
        }
        $requestData['slat'] = RandomUtil::alpha(6);
        $requestData['password'] = CommonUtil::encodePassword($requestData['password'], $requestData['slat']);
        //  如果没有上传头像,则使用默认头像
        if (empty($requestData['avatar'])) $requestData['avatar'] = Env::get("common.default_avatar");
        $requestData["create_time"] = date("Y-m-d H:i:s");
        unset($requestData['password_confirm']);
        $insertId = $this->model->insert($requestData, false, true);
        if (empty($insertId)) throw new CustomerException("新增管理员失败");
    }

    public function update()
    {
        $requestData = $this->request->except(["username"]);
        $validate = new \app\common\validate\Admin();
        $flag = $validate->scene("update")->check($requestData);
        if ($flag === false) {
            throw new ValidateException($validate->getError());
        }
        if (empty($requestData['password'])) {
            unset($requestData['password']);
        } else {
            $requestData['slat'] = RandomUtil::alpha(6);
            $requestData['password'] = CommonUtil::encodePassword($requestData['password'], $requestData['slat']);
        }
        unset($requestData['password_confirm']);
        $requestData["update_time"] = date("Y-m-d H:i:s");
        $count = $this->model->where("id", $requestData['id'])->update($requestData);
        if ($count > 1) throw new CustomerException("更新管理员失败");
        Cache::rm($this->model->getName() . ":" . $requestData["id"]);
    }

    public function managerInfo()
    {
        $adminUser = session("backend_admin_user");
        $adminUser['roles'] = [
            'admin'
        ];
        return $adminUser;
    }


    public function updatePassword()
    {
        $requestData = $this->request->only(['oldPassword', 'newPassword', 'newPassword2']);
        if (empty($requestData['oldPassword'])) throw new CustomerException("请输入旧密码");
        if (empty($requestData['newPassword'])) throw new CustomerException("请输入新密码");
        if (empty($requestData['newPassword2'])) throw new CustomerException("请输入确认密码");
        if ($requestData['newPassword2'] != $requestData['newPassword']) throw new CustomerException("两次密码输入必须一致");
        $adminUser = session("backend_admin_user");
        $adminUser = $this->model->findById($adminUser['id']);
        $encodePassword = CommonUtil::encodePassword($requestData['oldPassword'], $adminUser['salt']);
        if ($encodePassword !== $adminUser['password']) throw new CustomerException("旧密码错误");
        $password = CommonUtil::encodePassword($requestData['newPassword'], $adminUser['salt']);
        $this->model->where("id", $adminUser['id'])->update([
            'password' => $password
        ]);
        Cache::rm($this->model->getName() . ":" . $adminUser["id"]);
        return "修改密码成功";
    }

}